package com.dao;

import com.beans.CateInfo;
import com.jdbc.DBUtil;
import jdk.nashorn.internal.codegen.DumpBytecode;

import java.util.List;

public class CateDao {

    //通过父类id获取分类列表
    public List<CateInfo> getCateByParentId(int parentId) {
        String sql = "select * from cateInfo where parentId=?";
        return DBUtil.getList(sql,CateInfo.class,parentId);
    }

    //添加分类
    public int addCate(int parentId, String cateName, String des) {
        String sql = "insert into cateInfo (cateName,parentId,des) value(?,?,?)";
        return DBUtil.update(sql,cateName,parentId,des);
    }

    //获取所有分类
    public List<CateInfo> getAllCate() {
        String sql = "select * from cateInfo";
        return DBUtil.getList(sql,CateInfo.class);
    }

    public CateInfo getCateInfoById(int id) {
        String sql = "select * from cateInfo where id=?";
        return DBUtil.getSingleObj(sql,CateInfo.class,id);
    }

    public int deleteCateById(int id) {
        String sql = "delete from cateInfo where id=?";
        return DBUtil.update(sql,id);
    }

    public int updateBigCate(int id, String cateName, String des) {
        String sql = "update cateInfo set cateName=?,des=? where id=?";
        return DBUtil.update(sql,cateName,des,id);
    }

    public int updateSmallCate(int id, String cateName, String des, int parentId) {
        String sql = "update cateInfo set cateName=?,des=?,parentId=? where id=?";
        return DBUtil.update(sql,cateName,des,parentId,id);
    }

    //删除大分类时同时删除下面的小分类
    public int deleteCateUnderId(int id) {
        String sql = "delete from cateInfo where parentId=?";
        return DBUtil.update(sql,id);
    }
}
